<?php
namespace app\before\controller;

class AdPrice extends Base
{
    private $tn = 'ad_price';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            
            //$where['city'] = ['in',$this->admin_area];
            
            if(isset($this->get['position'])){
                $where['foreign_key'] = $this->get['position'];
            }
            if(isset($this->get['city'])){
                $where['city'] = $this->get['city'];
            }
            $list  = db($this->tn)->field(true)->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();

            
            foreach ($list as $k => $v) {
                $list[$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
                //$list[$k]['city'] = get_field('bz_area', $v['city']);
                $list[$k]['foreign_key'] = get_field('bz_ad_position', $v['foreign_key'],'position');
            }
            $count = db($this->tn)->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        $ad_position = db('ad_position')->select();
        $area = db('area')->where('id','in',$this->admin_area)->select();
        return $this->fetch('', ['area'=>$area,'ad_position'=>$ad_position]);
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            if(floatval($this->post['price']) <= 0){
                $this->returnAPI('价格有误');
            }
          
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误,请稍后重试');
                }
            }
            $is_exists = db($this->tn)->where('foreign_key',$this->post['foreign_key'])->where('city',0)->where('id','neq',$id)->find();
            if($is_exists){
                $this->returnAPI('广告位已经存在');
            }
            
            
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            if ($result !== false) {
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        //广告位
        $ad_position = db('ad_position')->select();
        $area = db('area')->where('id','in',$this->admin_area)->select();
        return $this->fetch('', ['data' => $data,'area'=>$area,'ad_position'=>$ad_position]);
    }

    public function delImg($path='',$id=''){
        if(!db($this->tn)->where('id',$id)->update(['default'=>''])){
            $this->returnAPI('删除失败，请稍后再试');
        }
        if(file_exists('.'.$path)){
            unlink('.'.$path);
        }
        $this->returnAPI('删除成功',0);
    }
}
